package com.jooq.cn;

import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;

import java.sql.Connection;
import java.sql.DriverManager;

import static test.generated.tables.Author.AUTHOR;

public class Main {

    public static void main(String[] args) {

        //用户名
        String userName = "root";
        //密码
        String password = "root";
        //mysql链接url
        String url = "jdbc:mysql://localhost:3306/library";
        Connection conn;
        try {
             conn = DriverManager.getConnection(url, userName, password);
            DSLContext using = DSL.using(conn, SQLDialect.MYSQL);
            Result<Record> fetch = using.select().from(AUTHOR).fetch();

            for (Record record : fetch) {
                Integer id = record.getValue(AUTHOR.ID);
                String firstName = record.getValue(AUTHOR.FIRST_NAME);
                String lastName = record.getValue(AUTHOR.LAST_NAME);
                /**
                 * 控制台输出
                 * ID: 1 first name: 3 last name: zhang
                 * ID: 2 first name: 4 last name: li
                 */
                System.out.println("ID:"+id + "firstName"+ firstName + "lastName: " + lastName);
            }
            //关闭链接
            conn.close();

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
